package org.brisling.sysman.service;

import java.util.List;

import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.JsonUtil;
import org.brisling.sysman.domain.Department;
import org.brisling.sysman.domain.DepartmentExtend;
import org.brisling.sysman.queryService.QueryDeptService;
import org.brisling.sysman.repository.DepartmentExtendRepo;
import org.brisling.sysman.repository.DepartmentRepo;
import org.brisling.sysman.service.base.BaseSysmanTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/department")
public class DepartmentService extends BaseSysmanTreeService<Department> {
	
	@SuppressWarnings("unused")
	@Autowired
	private DepartmentRepo baseRepo;
	
	@Autowired
	private DepartmentExtendRepo extendRepo;

	@Autowired
	private QueryDeptService qryDeptService;
	
	public DepartmentService(){
		this.moduleName = ConstSysParam.MODULE_DEPARTMENT;
		this.clazz = Department.class;
	}
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getMainPage()
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}	
	
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getTreeNode(java.lang.Long)
	 */
	@RequestMapping("/getTree.action")
	public List<Department> getTreeNode(@RequestParam(value="id",required=false) Long id){
		return (List<Department>) super.getNode(id);
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delFlowRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		
			return delRow(id,ids);
		
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#save(java.lang.String)
	 */
	@RequestMapping("/save.action")
	public Object save(@RequestBody String body){
		Department obj = this.bodyToObj(body, Department.class);
		if(obj!=null ){
			if(obj.getExtend()!=null)
				return this.saveExtend(body);
			else
				return super.save(obj);
		}		
		return null;
	}
	
	/**
	 * 查询部门扩展属性
	 * @param body
	 * @return
	 */
	@RequestMapping("/saveExtend.action")
	public DepartmentExtend saveExtend(@RequestBody String body){
		DepartmentExtend obj = JsonUtil.body2Class(body, DepartmentExtend.class);
		obj =extendRepo.save(obj);
		if(obj!=null && obj.getId()!=null)
			obj  = qryDeptService.getExtendOne(obj.getId());
		return obj;
	}

}
